/*******************************************************************************
* Project: QSAR(Cute Software Architecture)
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: E2EType.h
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: E2E
*
*******************************************************************************/

#ifndef E2ETYPE_H
#define E2ETYPE_H

/** HEADER FILES **/
#include "PlatformCfg.h" 


/** MACRO DEFINITION **/


/** TYPEDEF DEFINITION **/
typedef uint8 E2E_PCheckStatusType;
/* OK: the checks of the Data in this cycle were successful (including counter check) */
#define E2E_P_OK ((E2E_PCheckStatusType)0x00)
/* Data has a repeated counter. */
#define E2E_P_REPEATED ((E2E_PCheckStatusType)0x01)
/* The checks of the Data in this cycle were successful, with the exception of counter jump, 
which changed more than the allowed delta */
#define E2E_P_WRONGSEQUENCE ((E2E_PCheckStatusType)0x02)
/* Error not related to counters occurred (e.g. wrong crc, wrong length, wrong Data ID) or the return of 
the check function was not OK. */
#define E2E_P_ERROR ((E2E_PCheckStatusType)0x03)
/* No value has been received yet (e.g. during initialization). This is used as the initialization 
value for the buffer, it is not returned by any E2E profile */
#define E2E_P_NOTAVAILABLE ((E2E_PCheckStatusType)0x04)
/* No new data is available */
#define E2E_P_NONEWDATA ((E2E_PCheckStatusType)0x05)

typedef struct
{
    uint8 WindowSizeValid;
    uint8 MinOkStateInit;
    uint8 MaxErrorStateInit;
    uint8 MinOkStateValid;
    uint8 MaxErrorStateValid;
    uint8 MinOkStateInvalid;
    uint8 MaxErrorStateInvalid;
    uint8 WindowSizeInit;
    uint8 WindowSizeInvalid;
    boolean ClearToInvalid;
}E2E_SMConfigType;

typedef uint8 E2E_SMStateType;
/* Communication functioning properly according to E2E,  data can be used */
#define E2E_SM_VALID ((E2E_SMStateType)0x00)
/* State before E2E_SMCheckInit() is invoked, data cannot be used */
#define E2E_SM_DEINIT ((E2E_SMStateType)0x01)
/* No data from the sender is available since the initialization, data cannot be used */
#define E2E_SM_NODATA ((E2E_SMStateType)0x02)
/* There has been some data received since startup, but it is not yet possible use it, data cannot be used */
#define E2E_SM_INIT ((E2E_SMStateType)0x03)
/* Communication not functioning properly, data cannot be used */
#define E2E_SM_INVALID ((E2E_SMStateType)0x04)

typedef struct
{
    uint8* ProfileStatusWindow;
    uint8 WindowTopIndex;
    uint8 OkCount;
    uint8 ErrorCount;
    E2E_SMStateType SMState;
}E2E_SMCheckStateType;


typedef uint8 E2E_P01CheckStatusType;
#define E2E_P01STATUS_OK ((E2E_P01CheckStatusType)0x00)
#define E2E_P01STATUS_NONEWDATA ((E2E_P01CheckStatusType)0x01)
#define E2E_P01STATUS_WRONGCRC ((E2E_P01CheckStatusType)0x02)
#define E2E_P01STATUS_SYNC  ((E2E_P01CheckStatusType)0x03)
#define E2E_P01STATUS_INITIAL  ((E2E_P01CheckStatusType)0x04)
#define E2E_P01STATUS_REPEATED ((E2E_P01CheckStatusType)0x08)
#define E2E_P01STATUS_OKSOMELOST  ((E2E_P01CheckStatusType)0x20)
#define E2E_P01STATUS_WRONGSEQUENCE ((E2E_P01CheckStatusType)0x40)

typedef struct
{
    uint8 LastValidCounter;
    uint8 MaxDeltaCounter;
    boolean WaitForFirstData;
    boolean NewDataAvailable;
    uint8 LostData;
    E2E_P01CheckStatusType Status;
    uint8 SyncCounter;
    uint8 NoNewOrRepeatedDataCounter;
}E2E_P01CheckStateType;

typedef uint8 E2E_P01DataIDMode;
#define E2E_P01_DATAID_BOTH ((E2E_P01DataIDMode)0u)
#define E2E_P01_DATAID_ALT ((E2E_P01DataIDMode)1u)
#define E2E_P01_DATAID_LOW ((E2E_P01DataIDMode)2u)
#define E2E_P01_DATAID_NIBBLE ((E2E_P01DataIDMode)3u)

typedef struct
{
    /* Bit offset of Counter in MSB first order. CounterOffset shall be a multiple 
       of 4. In variants 1A, 1B, and 1C, CounterOffset is 8 */
    uint16 CounterOffset;
    /*  Bit offset of CRC (i.e. since *Data) in MSB first order. The offset shall be 
        a multiple of 8. In variants 1A, 1B, and 1C, CRCOffset is 0. */
    uint16 CRCOffset;
    /* A unique identifier, for protection against masquerading.  */
    uint16 DataID;
    /* Bit offset of the low nibble of the high byte of Data ID */
    uint16 DataIDNibbleOffset;
    /* Inclusion mode of ID in CRC computation (both bytes, alternating, or low byte only of ID included) */
    E2E_P01DataIDMode DataIDMode;
    /* Length of data, in bits. The value shall be a multiple of 8 */
    uint16 DataLength;
    /* Initial maximum allowed gap between two counter values of two consecutively received valid Data. */
    uint8 MaxDeltaCounterInit;
    /* The maximum amount of missing or repeated Data which the receiver does not expect to exceed under normal communication conditions */
    uint8 MaxNoNewOrRepeatedData;
    /* Number of Data required for validating the consistency of the counter that 
       must be received with a valid counter (i.e. counter within the allowed lock
       in range) after the detection of an unexpected behavior of a received 
       counter. */
    uint8 SyncCounterInit;
}E2E_P01ConfigType;

typedef struct
{
    /* Counter to be used for protecting the next Data. The initial value is 0, 
       which means that the first Data will have the counter 0. After the 
       protection by the Counter, the Counter is incremented modulo 0xF. The 
       value 0xF is skipped (after 0xE the next is 0x0), as 0xF value represents 
       the error value. The four high bits are always 0. */
    uint8 Counter;
}E2E_P01ProtectStateType;

#define E2E_E_OK ((Std_ReturnType)0x00)
#define E2E_E_INPUTERR_WRONG ((Std_ReturnType)0x01)
#define E2E_E_WRONGSTATE ((Std_ReturnType)0x02)
#define E2E_E_INPUTERR_NULL ((Std_ReturnType)0x03)
#define E2E_E_INTERR ((Std_ReturnType)0x04)


#endif

